/**
 * 商家服务类
 * 用于获取商家列表、商家详情等商家相关操作
 */

const request = require('../libs/request');

class BizGoodsService {
	// 获取商家商品列表
	static async getGoodsList(bizId) {
		return request({
			url: '/v1/biz-goods/index',
			data: { id: bizId },
			method: 'GET'
		});
	}

	// 获取商品详情
	static async getGoodsDetail(id) {
		const response = await request.get(`/v1/biz-goods/detail`, {
            needAuth: false,
            data: {
                id
            }
        });

        return {
            success: true,
            data: response.data,
            message: response.message || '获取商家详情成功'
        };
	}

	// 购买商品
	static async buyGoods(data) {
		const response = await request.authPost(`/v1/biz-goods/buy`,data);

        return {
            success: true,
            data: response.data,
            message: response.message
        };
	}

	// 获取最近商品订单记录
	static async getRecentOrders(goodsId) {
		const response = await request.get(`/v1/biz-goods/recent-orders`, {
			needAuth: false,
			data: { goods_id:goodsId }
		});

		return {
			success: true,
			data: response.data,
			message: response.message
		};
	}

	// 获取消费订单列表
	static async getOrderList(page = 1, limit = 10,range='all',keyword='') {
		const response = await request.get(`/v1/biz-goods/getOrderList`, {
			needAuth: true,
			data: { 
				page: page,
				limit: limit,
				range:range,
				keyword:keyword
			}
		});

		return {
			success: response.code === 1,
			data: response.data,
			message: response.msg || '获取消费订单列表成功'
		};
	}

	// 获取所有商家商品列表
	static async getAllGoodsList(page = 1, limit = 10) {
		const response = await request.get(`/v1/biz-goods/getAllGoodsList`, {
			needAuth: false,
			data: { 
				page: page,
				limit: limit
			}
		});

		return {
			success: response.code === 1,
			data: response.data,
			message: response.msg || '获取商品列表成功'
		};
	}

	static async goodsPings(params = {}) {
		const { id, page = 1, 
			pageSize = 10, showLoading = false } = params;
		try {
			const response = await request.get(`/v1/biz-goods/pings`, {
				showLoading,
				needAuth: false,
				data: {
					id,
					page,
					limit: pageSize
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取商家商品列表成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取商家商品列表失败'
			};
		}
	}

	static async searchGoods(params = {}) {
		const { keyword, page = 1, pageSize = 10, showLoading = false } = params;
		try {
			const response = await request.get(`/v1/biz-goods/search`, {
				showLoading,
				needAuth: false,
	
				data: {
					keyword,
					page,
					pageSize
				}
			});

			return {
				success: true,
				data: response.data,
				total: response.total,
				message: response.message || '搜索商家商品成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '搜索商家商品失败'
			};
		}
	}
}

module.exports = BizGoodsService;
